
//
//  HYOptionalView.m
//  LEVE
//
//  Created by 上官惠阳 on 2017/3/2.
//  Copyright © 2017年 dashuju. All rights reserved.
//

#import "HYOptionalView.h"
#import "HYOptionalViewItemView.h"

#define selfWidth (self.frame.size.width)
#define kLineViewHeight 2.5
#define KSpa 10
@interface HYOptionalView () <UIScrollViewDelegate>
@property (nonatomic, weak) UIScrollView *scrollView;
@property (nonatomic, weak) CALayer *lineLayer;
@property (nonatomic, assign) NSInteger currentIndex;
@property (nonatomic, strong) UIView *lineView;//底部滚动条
@end
@implementation HYOptionalView
// 设置偏移量
- (void)setContentOffset:(CGPoint)contentOffset {
    _contentOffset = contentOffset;

    CGFloat offsetX = contentOffset.x;
    // 当前索引
    NSInteger index = offsetX / kScreenWidth;

    if (_seletedColor) {
        self.lineView.backgroundColor = _seletedColor;
    }else{
        self.lineView.backgroundColor = kCommonHighLightRedColor;
    }

    CGFloat floatIndex = offsetX / kScreenWidth;
    CGFloat itemWidth = selfWidth / self.titles.count;
    self.lineView.left = KSpa + itemWidth * floatIndex;

    HYOptionalViewItemView *leftItem = (HYOptionalViewItemView *)[self.scrollView viewWithTag:index + 1];
    // 下一个按钮 如果rightBtnLeftDelta > 0则下一个按钮开始渲染
    HYOptionalViewItemView *rightItem = (HYOptionalViewItemView *)[self.scrollView viewWithTag:index + 2];

    // right
    // 相对于当前屏幕的宽度
    CGFloat rightPageLeftDelta = offsetX - index * kScreenWidth;
    CGFloat progress = rightPageLeftDelta / kScreenWidth;

    if ([leftItem isKindOfClass:[HYOptionalViewItemView class]]) {
        if (_seletedColor) {
            leftItem.textColor = _seletedColor;
        }else{
            leftItem.textColor = kCommonHighLightRedColor;
        }
        if (_normalColor) {
            leftItem.fillColor = _normalColor;
        }else{
            leftItem.fillColor = kCommonBlackColor;
        }

        leftItem.progress = progress;
    }
    if ([rightItem isKindOfClass:[HYOptionalViewItemView class]]) {
        if (_normalColor) {
            rightItem.textColor = _normalColor;
        }else{
            rightItem.textColor = kCommonBlackColor;
        }

        if (_seletedColor) {
            rightItem.fillColor = _seletedColor;
        }else{
            rightItem.fillColor = kCommonHighLightRedColor;
        }

        rightItem.progress = progress;
    }

    for (HYOptionalViewItemView *itemView in self.scrollView.subviews) {
        if ([itemView isKindOfClass:[HYOptionalViewItemView class]]) {
            if (itemView.tag != index + 1 && itemView.tag != index + 2) {
                if (_normalColor) {
                    itemView.textColor = _normalColor;
                }else{
                    itemView.textColor = kCommonBlackColor;
                }

                if (_seletedColor) {
                    itemView.fillColor = _seletedColor;
                }else{
                    itemView.fillColor = kCommonHighLightRedColor;
                }
                itemView.progress = 0.0;
            }
        }
    }

    // 重置当前索引
    self.currentIndex = index;
}

- (void)setTitles:(NSArray *)titles {
    _titles = titles;

    // 标题
    if (titles.count) {
        for (int i = 0; i < titles.count; i++) {
            HYOptionalViewItemView *item = [[HYOptionalViewItemView alloc] init];
            [self.scrollView addSubview:item];
            item.text = titles[i];
            item.tag = i + 1;
            item.textAlignment = NSTextAlignmentCenter;
            item.font = [UIFont boldSystemFontOfSize:16];
            item.userInteractionEnabled = YES;
            if(_normalColor){
                item.textColor = _normalColor;
            }
            [item addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(itemTapGest:)]];
        }
    }

    // 位置
    if (self.titles.count) {
        self.scrollView.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height - kLineHeight);
        self.scrollView.contentSize = CGSizeMake(60 * self.titles.count, self.scrollView.height - kLineHeight);
        CGFloat btnW = self.scrollView.contentSize.width / self.titles.count;
        for (int i = 0 ; i < self.titles.count; i++) {
            HYOptionalViewItemView *item = (HYOptionalViewItemView *)[self.scrollView viewWithTag:i + 1];
            item.frame = CGRectMake(btnW * i, 0, btnW, self.scrollView.height);
        }
    }
    // 分割线
    self.lineLayer.frame = CGRectMake(0, self.scrollView.height - kLineHeight, selfWidth, kLineHeight);
    self.lineView.frame = CGRectMake(KSpa, self.scrollView.height - 5, selfWidth / titles.count - 2*KSpa, kLineViewHeight);
    self.lineView.layer.masksToBounds = YES;
    self.lineView.layer.cornerRadius = kLineViewHeight / 2.0;
}

// 点击item执行回调
- (void)itemTapGest:(UITapGestureRecognizer *)tapGest {
    HYOptionalViewItemView *item = (HYOptionalViewItemView *)tapGest.view;
    if (item) {

        if (self.optionalViewItemClickHandle) {
            self.optionalViewItemClickHandle(self, item.text, item.tag - 1);
        }
        self.currentIndex = item.tag - 1;
    }
}

- (UIScrollView *)scrollView {
    if(!_scrollView){
        UIScrollView *sc = [[UIScrollView alloc] init];
        sc.delegate = self;
        [self addSubview:sc];
        _scrollView = sc;
        sc.backgroundColor = [UIColor clearColor];
        sc.showsVerticalScrollIndicator = NO;
        sc.showsHorizontalScrollIndicator = NO;
    }
    return _scrollView;
}

- (CALayer *)lineLayer {
    if (!_lineLayer) {
        CALayer *line = [CALayer layer];
        [self.scrollView.layer addSublayer:line];
        line.backgroundColor = kSeperatorColor.CGColor;
        _lineLayer = line;
    }
    return _lineLayer;
}
- (UIView *)lineView
{
    if (!_lineView) {
        UIView *lineView = [[UIView alloc] init];
        _lineView = lineView;
        [self addSubview:lineView];
    }
    return _lineView;
}
- (void)setShowBottomLine:(BOOL)showBottomLine
{
    _showBottomLine = showBottomLine;
    self.lineLayer.hidden = !showBottomLine;
}
@end
